<script setup lang="ts">
import { getCurrentInstance, reactive, onMounted, nextTick } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
const { proxy } = getCurrentInstance()

/* ---- */
const logUser = proxy.$Store.get('logUser') || {}
const WXConfig = proxy.$Store.get('WXConfig') || {}
const forSys = proxy.$Sys || []

/* --设置微信分享功能-- */
proxy.$WxTool.loadJsdk(WXConfig, (obj) => {
  if (!obj) return
  // ----
  obj.hideMenuItem()
})

/* ---- */
const user = reactive({
  info: '',
  // ----
  title: ''
})
// ----
const items = [{
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '表格设置',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'MaterialAdm') }
  }]
}, {
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '添加管理',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'MaterialGoods') }
  }]
}, {
  show: true,
  child: [{
    show: true,
    isArrow: true,
    name: '预览分享',
    callback: () => { proxy.$Myfun.openPage(proxy, 'push', 'MaterialShare', { dealer_id: logUser.id }) }
  }]
}]

/* ---- */
const setAppTitle = (title) => {
  document.title = title
  // --利用iframe的onload事件刷新页面--
  const iframe = document.createElement('iframe')
  iframe.style.visibility = 'hidden'
  iframe.style.width = 0
  iframe.style.height = 0
  document.body.appendChild(iframe)
  nextTick(() => { document.body.removeChild(iframe) })
}
// ----
const getCurrencyInfo = async () => {
  // --获取定制功能--
  const customItems = forSys.filter((item) => { return String(item.name) === 'market_customFuncUser' })
  if (Number(customItems.length) === 1) {
    proxy.$Myfun.getJsonData(customItems[0].value, (resJson) => {
      if (Number(resJson.length) > 0) {
        if (resJson[0].material && Number(resJson[0].material.length) > 0) {
          resJson[0].material.some((item) => {
            if (String(item.telphone) === String(logUser.telphone)) user.info = item.name
          })
        }
      }
    })
  }
  // ----
  user.title = user.info + document.title
  // --设置网页title--
  const forTitle = user.title
  setAppTitle(forTitle)
}
// ----
const getAuditFun = (type, item) => {
  switch (Number(type)) {
    case 1: {
      item.callback && item.callback(item)
      break
    }
  }
}

/* ---- */
onMounted(() => {
  getCurrencyInfo()
})
onBeforeRouteLeave(() => { proxy.$Myfun.setScrollTop(proxy.$refs.formSection) })
</script>

<template>
  <div class="mescroll-touch" ref="formSection">
    <ul class="userSys xw-flex xw-flex-column">
      <template v-for="(item, index) in items">
      <li v-if="item.show">
        <template v-for="(son, num) in item.child">
        <a class="xw-Tfg xw-flex xw-flex-align" @click="getAuditFun(1, son)" v-if="son.show">
          <div class="USS-title xw-flex-W1">{{son.name}}</div>
          <i class="fal fa-angle-right" v-if="son.isArrow"></i>
        </a>
        </template>
      </li>
      </template>
    </ul>
  </div>
</template>
